package cn.wolfcode.wolf2w.service.impl;

import ch.qos.logback.core.db.DBHelper;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import com.mongodb.BasicDBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
//@Transactional
public class TravelCommentServiceImpl implements ITravelCommentService {

    //操作MongoDB数据库
    @Autowired
    private TravelCommentRepository travelCommentRepository;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void save(TravelComment travelComment) {
        /**
         * 保证id为空
         * 维护的字段:
         * city(城市)
         * type(评论级别)
         * createTime(发表时间)
         * refComment
         *
         */
        travelComment.setId(null);
        travelComment.setCity(travelService.findByOne(travelComment.getTravelId()).getDestName());
        travelComment.setCreateTime(new Date());
        String refId = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(refId)) {
            TravelComment queryTravelComment = this.findByOne(refId);
            travelComment.setRefComment(queryTravelComment);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        } else {
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    

    /**
     * 高查
     * @param qo
     * @return
     */
    @Override
    public Object query(TravelCommentQuery qo) {
        //创建查询对象
        Query query = new Query();
        //总条数
        long count = mongoTemplate.count(query, TravelComment.class);
        if (count==0) {
            //假设查询条数为0,返回空内容
            return Page.empty();
        }
        /*
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,pageable, query,TravelComment.class, qo);*/
        //查询指定游记明细
        query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));

        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "createTime");
//        query.with(pageRequest);
        //升序
        query.with(Sort.by(Sort.Order.asc("createTime")));

        //查询数据库(MongoDB)
        List<TravelComment> list = mongoTemplate.find(query, TravelComment.class);
//        return new PageImpl<>(list,pageRequest,count);
        return list;
    }

    @Override
    public TravelComment findByOne(String id) {
        return travelCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> findAll() {
        return travelCommentRepository.findAll();
    }
}
